package dao;

import entity.FinancingProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FinancingProductDao extends BaseDao {

    public List<FinancingProduct> getAllProducts() throws SQLException {
        List<FinancingProduct> products = new ArrayList<>();
        Connection conn = getConnection();
        String sql = "SELECT * FROM FinancingProduct";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            FinancingProduct product = new FinancingProduct();
            product.setId(rs.getString("Id"));
            product.setRisk(rs.getInt("Risk"));
            product.setIncome(rs.getString("Income"));
            product.setSaleStarting(rs.getDate("SaleStarting"));
            product.setSaleEnd(rs.getDate("SaleEnd"));
            product.setEnd(rs.getDate("End"));
            products.add(product);
        }
        closeAll(conn, stmt, rs);
        return products;
    }

    public List<FinancingProduct> searchByCriteria(String keyword, int risk) throws SQLException {
        List<FinancingProduct> products = new ArrayList<>();
        Connection conn = getConnection();
        String sql = "SELECT * FROM FinancingProduct WHERE Id LIKE ?";
        if (risk != -1) {
            sql += " AND Risk = ?";
        }
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, "%" + keyword + "%");
        if (risk != -1) {
            stmt.setInt(2, risk);
        }
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            FinancingProduct product = new FinancingProduct();
            product.setId(rs.getString("Id"));
            product.setRisk(rs.getInt("Risk"));
            product.setIncome(rs.getString("Income"));
            product.setSaleStarting(rs.getDate("SaleStarting"));
            product.setSaleEnd(rs.getDate("SaleEnd"));
            product.setEnd(rs.getDate("End"));
            products.add(product);
        }
        closeAll(conn, stmt, rs);
        return products;
    }

    public boolean addProduct(FinancingProduct product) throws SQLException {
        Connection conn = getConnection();
        String sql = "INSERT INTO FinancingProduct (Id, Risk, Income, SaleStarting, SaleEnd, End) VALUES (?, ?, ?, ?, ?, ?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, product.getId());
        stmt.setInt(2, product.getRisk());
        stmt.setString(3, product.getIncome());
        stmt.setDate(4, product.getSaleStarting());
        stmt.setDate(5, product.getSaleEnd());
        stmt.setDate(6, product.getEnd());
        int rowsAffected = stmt.executeUpdate();
        closeAll(conn, stmt, null);
        return rowsAffected > 0;
    }

    public boolean modifyProduct(FinancingProduct product) throws SQLException {
        Connection conn = getConnection();
        String sql = "UPDATE FinancingProduct SET Risk = ?, Income = ?, SaleStarting = ?, SaleEnd = ?, End = ? WHERE Id = ?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, product.getRisk());
        stmt.setString(2, product.getIncome());
        stmt.setDate(3, product.getSaleStarting());
        stmt.setDate(4, product.getSaleEnd());
        stmt.setDate(5, product.getEnd());
        stmt.setString(6, product.getId());
        int rowsAffected = stmt.executeUpdate();
        closeAll(conn, stmt, null);
        return rowsAffected > 0;
    }

    public boolean deleteProduct(String id) throws SQLException {
        Connection conn = getConnection();
        String sql = "DELETE FROM FinancingProduct WHERE Id = ?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, id);
        int rowsAffected = stmt.executeUpdate();
        closeAll(conn, stmt, null);
        return rowsAffected > 0;
    }
}
